package com.glorysoft.sunnypda.ui.searcheqp

import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.annotation.RequiresApi
import com.glorysoft.sunnypda.BR
import com.glorysoft.sunnypda.R
import com.glorysoft.sunnypda.base.BaseActivity
import com.glorysoft.sunnypda.databinding.ActivitySearchEqpInfoBinding
import com.glorysoft.sunnypda.entity.ui.ToastMessageBean
import com.glorysoft.sunnypda.utils.PopWindowsUtils
import kotlinx.android.synthetic.main.activity_search_eqp_info.*

/**
 * FileName: SearchEqpInfoActivity
 * Author: XJH
 * Date: 2022/1/11 16:59
 * Description:
 * History:
 */
class SearchEqpInfoActivity : BaseActivity<SearchEqpInfoViewModel, ActivitySearchEqpInfoBinding>(),
    View.OnClickListener {

    private var areaPop: PopWindowsUtils.GeneralPop? = null
    private var fabPop: PopWindowsUtils.GeneralPop? = null

    override fun layoutId(): Int {
        return R.layout.activity_search_eqp_info
    }

    override fun initVariableId(): Int {
        return BR.searchEqpInfoViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.search_equipment))
        flAreaList.setOnClickListener(this)
        flFabList.setOnClickListener(this)
    }

    override fun initData() {
//        viewModel.getArea(this)
        viewModel.getFabList()
        viewModel.defUI.callObserve.observe(this) {
            tvPost.text = viewModel.eqpItems.size.toString()
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.flFabList -> {
                if (viewModel.fabList.isEmpty()) {
                    viewModel.defUI.toastEvent.value =
                        ToastMessageBean(getString(R.string.return_area_is_empty), false)
                    return
                }
                fabPop = PopWindowsUtils.getOneChoosePop(
                    this,
                    flFabList.width,
                    viewModel.fabList,
                    ivFabArrow,
                    object :
                        PopWindowsUtils.PopTextAndPositionCallback {
                        @RequiresApi(Build.VERSION_CODES.P)
                        override fun onSuccess(codeS: String, potion: Int) {
                            if (codeS == "null") {
                                tvFab.text = ""
                            } else {
                                tvFab.text = codeS
                            }
                            tvArea.text = ""
                            if (codeS != "") {
                                viewModel.getLineList(codeS.split(":")[1])
                            }
                        }

                    })


                if (fabPop!!.isShowing) {
                    fabPop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(ivFabArrow)
                    fabPop?.showAsDropDown(flFabList, 0, 0)
                }
            }
            R.id.flAreaList -> {
                if (viewModel.mAreaList.isEmpty()) {
                    viewModel.defUI.toastEvent.value =
                        ToastMessageBean(getString(R.string.return_area_is_empty), false)
                    return
                }
                areaPop = PopWindowsUtils.getOneChoosePop(
                    this,
                    flAreaList.width,
                    viewModel.mAreaList,
                    ivAreaArrow,
                    object :
                        PopWindowsUtils.PopTextAndPositionCallback {
                        @RequiresApi(Build.VERSION_CODES.P)
                        override fun onSuccess(codeS: String, potion: Int) {
                            tvArea.text = codeS
//                                viewModel.handler.removeCallbacksAndMessages("EQP_BY_AREA")
                            viewModel.getEqpByArea(tvFab.text.toString(), codeS)
                        }

                    })

                if (areaPop!!.isShowing) {
                    areaPop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(ivAreaArrow)
                    areaPop?.showAsDropDown(flAreaList, 0, 0)
                }
            }
        }
    }
}